package com.orhon.smartcampus.modules.systemctl.fetcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.systemctl.dao.OperationsDao;
import com.orhon.smartcampus.modules.systemctl.entity.Menus;
import com.orhon.smartcampus.modules.systemctl.entity.Modules;
import com.orhon.smartcampus.modules.systemctl.entity.Operations;
import com.orhon.smartcampus.modules.systemctl.entity.Widgets;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLMenus;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLModule;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLOperation;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLWidgets;
import com.orhon.smartcampus.modules.systemctl.mm.MenuMapper;
import com.orhon.smartcampus.modules.systemctl.mm.ModuleMapper;
import com.orhon.smartcampus.modules.systemctl.mm.OperationMapper;
import com.orhon.smartcampus.modules.systemctl.mm.WidgetsMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.R;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class OperationsFetcher implements DataFetcher {


    @Autowired
    OperationsDao operationsDao;

    @Autowired
    TokenService tokenService;

    /**
     * 获取权限列表
     *
     * @param environment
     * @return
     * @throws Exception editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        List<GQLOperation> operations = new ArrayList<>();

        String token = tokenService.getSystemToken();
        if(token==null) {
            return  R.NoLogin();
        }
        Long schoolId = JwtUtils.getSchoolId(token);

        GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(environment);
        String hql = "from Operations where deleted_at = null ";

        if (environment.containsArgument("condition")) {
            HashMap<String, Object> map = ObjectToMap.to(environment.getArgument("condition"));
            if (map.get("id") != null) {
                hql += " and id  "+ Util.decide (map.get("id"));
            }
            if (map.get("module_id") != null) {
                hql += " and module_id " + Util.decide(map.get("module_id"));
            }
            if (map.get("parent_id") != null) {
                hql += " and parent_id " + Util.decide(map.get("parent_id"));
            }

        }
        List<Operations> list = operationsDao.find(hql, null, page.getPage(), page.getLimit());


        for (Operations item : list) {
            GQLOperation gqlOperations = null;
            if (environment.getSelectionSet().contains("data/parent")) {
                Operations parent = item.getSystemctlOperationsByParentId();
                gqlOperations = OperationMapper.MAPPER.toOperations(parent);
            }
            GQLModule gqlModule = null;
            if (environment.getSelectionSet().contains("data/module")) {
                Modules modules = item.getSystemctlModulesByModuleId();
                gqlModule = ModuleMapper.MAPPER.mapModule(modules);
            }
            List<GQLOperation> gqlChildreen = null;
            if (environment.getSelectionSet().contains("data/childreen")) {
                List<Operations> childreen = item.getChildreen();
                gqlChildreen = OperationMapper.MAPPER.toOperationsList(childreen);
            }
            
            List<GQLMenus> gqlMenus = null;
            if (environment.getSelectionSet().contains("data/menus")) {
                List<Menus> menus = item.getMenus();
                gqlMenus = MenuMapper.MAPPER.toMenusList(menus);
            }
            
            List<GQLWidgets> gqlWidgets = null;
            if (environment.getSelectionSet().contains("data/widgets")) {
                List<Widgets> widgets = item.getWidgets();
                gqlWidgets = WidgetsMapper.MAPPER.toWidgetsList(widgets);
            }
            
            GQLOperation operation = OperationMapper.MAPPER.toOperations(item);
            operation.setModule(gqlModule);
            operation.setParent(gqlOperations);
            operation.setChildreen(gqlChildreen);
            operation.setMenus(gqlMenus);
            operation.setWidgets(gqlWidgets);
            operations.add(operation);
        }

        Long total = 0L;
        if (environment.getSelectionSet().contains("total")) {
            total = operationsDao.getTotalCount(hql);
        }
        return Result.ok().put("data", operations).put("total", total);
    }
}
